本文目录
1、GnuWin32下载地址
https://sourceforge.net/projects/getgnuwin32/files/latest/download
http://gnuwin32.sourceforge.net/x86_32
2、GnuWin32官方说明及使用方法
2.1、说明
The GnuWin project provides Win32-versions of GNU tools, or tools with a similar open source licence. The ports are native ports, that is they rely only on libraries provided with any standard 32-bits MS-Windows operating system, such as MS-Windows 95 / 98 / ME / NT / 2000 / XP / 2003 / Vista. Unlike CygWin or Msys, native ports do not rely on some kind of Unix emulation, so that there is no need to install additional emulation libraries. At present, all developments have been done under MS-Windows-XP, using the Mingw port of the GNU C and C++ (GCC) compilers. Utilities and libraries provided by GnuWin, are used and distributed with packages such as GNU Emacs and KDE-Windows.
2.2、安装和使用方法
下载网址:http://gnuwin32.sourceforge.net/packages/make.htm
其他组件包:http://gnuwin32.sourceforge.net/packages.html
具体安装可参考:https://stackoverflow.com/questions/2532234/how-to-run-a-makefile-in-windows
或:https://www.cnblogs.com/April-Chou-HelloWorld/p/7636300.html
3、windows下gnu makefile自动化编译
方法一: 采用GnuWin32提供的工具(https://sourceforge.net/projects/gnuwin32/files/make),解析makefile
或者直接将MinGW中的make.exe拷贝到脚本所在目录下,运行脚本即可。
参考自:https://blog.csdn.net/listener51/article/details/84933876
3.1、编写config_win.mk
存放相关系统宏和编译flags:
ifeq ($(strip $(platform)), x86_32)
CC := cl.exe
LD := link.exe
AR := lib.exe
RC := rc.exe
EXTRA_CFLAGS := -W3 -nologo -DWIN32
EXTRA_CFLAGS += -D_CRT_SECURE_NO_WARNINGS -O2
EXTRA_LDFLAGS :=
EXTRA_RCFLAGS := -DWIN32
endif
ifeq ($(strip $(platform)), x86_64)
CC := cl.exe
LD := link.exe
AR := lib.exe
RC := rc.exe
EXTRA_CFLAGS := -W3 -nologo -DWIN64
EXTRA_CFLAGS += -D_CRT_SECURE_NO_WARNINGS -O2
EXTRA_LDFLAGS :=
EXTRA_RCFLAGS := -DWIN64
endif
CFLAGS := $(EXTRA_CFLAGS)
LDFLAGS := $(EXTRA_LDFLAGS)
RCFLAGS := $(EXTRA_RCFLAGS)
ARFLAGS :=
3.2、编写makefile_win.mk
###windows gnu make
### 假设库的目录结构如下且同一“-”长度的属于同一目录
### -3rdparty #第三方库相关的库文件
### -src #源码路径(包含C文件及头文件)
### -build #build目录下有各平台相关的文件夹
### --windows #windows文件夹
### ---makefile #windows下的makefile
### ---dll_info.rc #资源文件
### --objs #obj文件输出目录
### --out #库输出目录
include config_win.mk
##库的名字在此设置
LIB_NAME := xxx
##指定相关路径关系
ROOT_DIR := ../../
C_SRCS_DIR := $(ROOT_DIR)/src
INCS_DIR := $(ROOT_DIR)/src
OBJS_DIR := $(ROOT_DIR)/objs
INC_FLAGS := -I$(ROOT_DIR)/src \
-I$(ROOT_DIR)/3rdparty
#设置动态库静态库生成路径
TARGET_S := $(ROOT_DIR)/out/lib_$(LIB_NAME).lib #静态库
TARGET_D := $(ROOT_DIR)/out/$(LIB_NAME).dll #动态库
DLL_IMPLIB := $(ROOT_DIR)/out/$(LIB_NAME).lib #动态库导入库
DLL_LIBPDB := $(ROOT_DIR)/out/$(LIB_NAME).pdb #动态库的PDB文件
#设置资源文件路径
RC_SRCS := $(ROOT_DIR)/build/windows/dll_info.rc
#设置相关源文件
C_SRCS := $(C_SRCS_DIR)/xxx.c
#设置编译过程输出日志
OUTPUT = >>compile.log 2>&1
##目标文件
OBJS := $(patsubat %.s, %.obj, $(patsubat %.c, %.obj, $(patsubat %.cpp, %.obj, $(C_SRCS))))
OBJS_PATH := $(addprefix $(OBJS_DIR)/, $(notdir $(OBJS)))
##资源文件
OBJRES := $(patsubat %.rc, %.res, $(RC_SRCS))
OBJRES_PATH := $(addprefix $(OBJS_DIR)/, $(notdir $(OBJRES)))
all: clean $(TARGET_S) $(TARGET_D)
#静态库
$(TARGET_S):$(OBJS)
$(AR) $(ARFLAGS) -out:$@ $(OBJS_PATH) $(OUTPUT)
#动态库
$(TARGET_D):$(OBJS)
$(LD) $(LDFLAGS) -dll -implib:$(DLL_IMPLIB) -out:$@ $(OBJS_PATH) $(OBJRES_PATH) -debug -PDB:$(DLL_LIBPDB)
%.obj:%.c
$(CC) $(CFLAGS) $(INC_FLAGS) -Fo$(addprefix $(OBJ_DIR)/, $(@F)) -c $^
%.obj:%.cpp
$(CPP) $(CFLAGS) $(INC_FLAGS) -Fo$(addprefix $(OBJ_DIR)/, $(@F)) -c $^
%.res:%.rc
###SubWCRev.exe ../ ../dll_info.t ../dll_info.rc
$(RC) $(RCFLAGS) -Fo $(addprefix $(OBJS_PATH)/, $(@F)) $^
clean:
where -F -R . *.obj *.log | grep -e .obj -e .log | xargs rm -rf
rm -f $(OBJRES_PATH)
rm -f $(OBJS_PATH)
方法二:
在MinGW环境下进行Windows 平台GNU makefile的编写
3.3、参考
https://cognitivewaves.wordpress.com/makefiles-windows/
https://opensourceforu.com/2012/06/gnu-make-in-detail-for-beginners/
https://stackoverflow.com/questions/2532234/how-to-run-a-makefile-in-windows
https://stackoverflow.com/questions/2532234/how-to-run-a-makefile-in-windows
THE END !
本博文只能阅读,谢绝转载,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 2963033731@qq.com